from NorManager import Logger, Session, SessionWriter
import thread, time, random

class TipOfDay:
    """Tip of day example class."""

    def __init__(self, tipsfile, delay):
        self.tipsfile = tipsfile
        self.delay = delay

    def LoadTips(self):
        file = open("scripts/%s" % self.tipsfile, 'r')
        self.tips = file.readlines()
        file.close()

    def GetRandom(self):
        return self.tips[random.randint(0, len(self.tips)-1)]

    def PrintTips(self):
##      Prints tips from file. Every 20th loop reloads tips file
##      So it's possible to change tips without closing program
        def PrintThread():
            loop = 0
            while 1:
                if loop == 0:
                    self.LoadTips()
                time.sleep(self.delay)
                if Session.Instance.State.ToString() == "Running":
                    Session.Instance.Writer.Write("say \"%s\"" % self.GetRandom())
                if loop == 20:
                    loop = 0
                else:
                    loop += 1
        thread.start_new_thread(PrintThread, ())
        

##Set 15*60 sec loop delay
tipofday = TipOfDay("tips.txt", 15*60)
tipofday.PrintTips()
